﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;

namespace TcpTestServer.DataLayer
{
    class Message
    {
        public XElement Body
        {
            get
            {
                return _body;
            }
        }
        public int Number
        {
            get
            {
                return _number;
            }
        }
        public int ResponseTimeout
        {
            get
            {
                return _responseTimeout;
            }
        }
        public XmlSchema Schema
        {
            get
            {
                return _reply_schema;
            }
        }



        public Message(string name, List<Argument> output_arguments, List<Argument> reply_arguments, int number, int responseTimeout)
        {
            string message_template_xml = System.IO.File.ReadAllText(MainSettings.Default.XmlResourcesPath + MainSettings.Default.RequestMessageXml);
            string argument_template_xml = System.IO.File.ReadAllText(MainSettings.Default.XmlResourcesPath + MainSettings.Default.RequestArgumentXml);
            StringBuilder sb_arguments_xml = new StringBuilder();

            for (int i = 0; i < output_arguments.Count; i++)
                sb_arguments_xml.AppendLine(String.Format(argument_template_xml, output_arguments[i].Name, output_arguments[i].Value));

            string message_xml = String.Format(message_template_xml, name, number.ToString(), sb_arguments_xml.ToString());

            byte[] byteArray_xml = Encoding.ASCII.GetBytes(message_xml);
            System.IO.MemoryStream m_stream_xml = new System.IO.MemoryStream(byteArray_xml);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas.Add(getXsd(output_arguments, true));
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader_xml = XmlReader.Create(m_stream_xml, settings);
            _body = XElement.Load(reader_xml);
            _number = number;
            _reply_schema = getXsd(reply_arguments, false);
            _responseTimeout = responseTimeout;
        }
        public Message(string name, List<Argument> output_arguments, List<Argument> reply_arguments, int number)
        {
       
            string message_template_xml = System.IO.File.ReadAllText(MainSettings.Default.XmlResourcesPath + MainSettings.Default.RequestMessageXml);
            string argument_template_xml = System.IO.File.ReadAllText(MainSettings.Default.XmlResourcesPath + MainSettings.Default.RequestArgumentXml);
            StringBuilder sb_arguments_xml = new StringBuilder();

            for (int i = 0; i < output_arguments.Count; i++)
                sb_arguments_xml.AppendLine(String.Format(argument_template_xml, output_arguments[i].Name, output_arguments[i].Value));
            
            string message_xml = String.Format(message_template_xml,name,number.ToString(),sb_arguments_xml.ToString());
          
            byte[] byteArray_xml = Encoding.ASCII.GetBytes(message_xml);
            System.IO.MemoryStream m_stream_xml = new System.IO.MemoryStream(byteArray_xml); 
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas.Add(getXsd(output_arguments,true));
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader_xml = XmlReader.Create(m_stream_xml,settings);
            _body = XElement.Load(reader_xml);
            _number = number;
            _reply_schema = getXsd(reply_arguments, false);
            
        }

        private XmlSchema getXsd(List<Argument> arguments, bool is_input)
        {
            string message_template_xsd;
            string argument_template_xsd;

            if (is_input)
            {
                message_template_xsd = System.IO.File.ReadAllText(MainSettings.Default.XmlResourcesPath + MainSettings.Default.RequestMessageXsd);
                argument_template_xsd = System.IO.File.ReadAllText(MainSettings.Default.XmlResourcesPath + MainSettings.Default.ArgumentXsd);
            }
            else
            {
                message_template_xsd = System.IO.File.ReadAllText(MainSettings.Default.XmlResourcesPath + MainSettings.Default.ReplyMessageXsd);
                argument_template_xsd = System.IO.File.ReadAllText(MainSettings.Default.XmlResourcesPath + MainSettings.Default.ArgumentXsd);
            }
            StringBuilder sb_arguments_xsd = new StringBuilder();
            for (int i = 0; i < arguments.Count; i++)
                sb_arguments_xsd.AppendLine(String.Format(argument_template_xsd, arguments[i].Name, arguments[i].DataType));

            string schema_content = String.Format(message_template_xsd, sb_arguments_xsd.ToString());

            byte[] byteArray_xml = Encoding.ASCII.GetBytes(schema_content);
            System.IO.MemoryStream m_stream_xml = new System.IO.MemoryStream(byteArray_xml);
            XmlSchema schema = XmlSchema.Read(m_stream_xml,null);
            return schema;

        }
        


      
        XElement _body;
        XmlSchema _reply_schema ;
        int _number;
        int _responseTimeout = 10000;
    }
}
